package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.Singer;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class SingerAdapter extends BaseAdapter{
    private List<Singer.ArtistEntity> datas;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private Context mContext;

    public SingerAdapter(Context c) {

        mContext=c;
        imageLoader=VolleySingleton.getInstance(mContext).getImageLoader();

    }

    public void initData(List<Singer.ArtistEntity> datas) {
        if (this.datas==null){
            this.datas=new ArrayList<Singer.ArtistEntity>();
        }else {
            this.datas.clear();
        }
        this.datas=datas;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return datas!=null&&datas.size()>0?datas.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.activity_singer_item,null);
            viewHolder=new ViewHolder();
            viewHolder.singerImageView= (ImageView) convertView.findViewById(R.id.singerImageView);
            viewHolder.singerTextView= (TextView) convertView.findViewById(R.id.singerTextView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Singer.ArtistEntity entity= (Singer.ArtistEntity) getItem(position);
        Picasso.with(mContext).load(entity.getAvatar_big()).into(viewHolder.singerImageView);
//        ImageLoader.ImageListener imageListener=ImageLoader.getImageListener(
//                viewHolder.singerImageView,R.mipmap.background,R.mipmap.background);
//        imageLoader.get(entity.getAvatar_middle(),imageListener);
        viewHolder.singerTextView.setText(entity.getName());

        return convertView;
    }
    private class  ViewHolder{
        private ImageView singerImageView;
        private TextView singerTextView;

    }
}
